﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using HiTeachers.Entities;
using HiTeachers.Services;
using RJS.Web.WebControl;

namespace HiTeachers.Web
{
    public partial class ListMember : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnFullAccountClick(object sender, EventArgs e)
        {
            if (Request.Form["chkItem"] == null) return;

            string selectedItems = Request.Form["chkItem"];
            string[] selectedItemIds = selectedItems.Split(',');
            foreach (Teacher teacher in selectedItemIds.Select(t => new Guid(t)).Select(itemId => TeacherServices.GetById(itemId)))
            {
                teacher.IsFullAccount = !teacher.IsFullAccount;

                TeacherServices.SaveOrUpdate(teacher);
            }

            gridData.DataBind();
            udpGrid.DataBind();
        }

        protected void BtnDeleteClick(object sender, EventArgs e)
        {
            if (Request.Form["chkItem"] == null) return;

            string selectedItems = Request.Form["chkItem"];
            string[] selectedItemIds = selectedItems.Split(',');
            foreach (Teacher teacher in
                selectedItemIds.Select(t => new Guid(t)).Select(itemId => TeacherServices.GetById(itemId))) TeacherServices.Delete(teacher);

            gridData.DataBind();
            udpGrid.DataBind();
        }

        protected void GridDataItemCommand(object sender, CommandEventArgs e)
        {
            string commandName = e.CommandName;
            string commandArgument = e.CommandArgument.ToString();

            if (commandArgument == string.Empty) return;

            var guidId = new Guid(commandArgument);
            Teacher teacher = TeacherServices.GetById(guidId);

            switch (commandName)
            {
                case "Delete":
                    TeacherServices.Delete(teacher);

                    break;
                case "Full":
                    teacher.IsFullAccount = true;
                    TeacherServices.SaveOrUpdate(teacher);

                    break;
                case "NotFull":
                    teacher.IsFullAccount = false;
                    TeacherServices.SaveOrUpdate(teacher);

                    break;
            }
            udpGrid.DataBind();
        }

        protected void GridDataRowEditing(object sender, GridViewEditEventArgs e)
        {
            gridData.EditIndex = e.NewEditIndex;
            gridData.DataBind();
            udpGrid.DataBind();
        }

        protected void GridDataRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowState != DataControlRowState.Edit &&
                e.Row.RowState != (DataControlRowState.Alternate | DataControlRowState.Edit)) return;

            var teacher = (Teacher)e.Row.DataItem;
            ((PopCalendar)e.Row.FindControl("fromDateCalendar")).SetDateValue(teacher.ActivatedDate);
            ((PopCalendar)e.Row.FindControl("toDateCalendar")).SetDateValue(teacher.DeactivatedDate);
        }

        protected void GridDataRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gridData.EditIndex = -1;
            gridData.DataBind();
            udpGrid.DataBind();
        }

        protected void GridDataRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // ReSharper disable PossibleNullReferenceException
            var itId = new Guid(Convert.ToString(gridData.DataKeys[e.RowIndex].Value));
            // ReSharper restore PossibleNullReferenceException

            Teacher teacher = TeacherServices.GetById(itId);
            teacher.ActivatedDate = Convert.ToDateTime(((TextBox)gridData.Rows[e.RowIndex].FindControl("txtFromDate")).Text);
            teacher.DeactivatedDate = Convert.ToDateTime(((TextBox)gridData.Rows[e.RowIndex].FindControl("txtToDate")).Text);

            TeacherServices.SaveOrUpdate(teacher);

            gridData.EditIndex = -1;
            gridData.DataBind();
            udpGrid.DataBind();
        }
    }
}
